今日本篇重點是要安裝Heartbeat,以如何設定要監控的服務項目。
步驟一: 下載 Heartbeat 安裝包, 下載地址。
curl -L -O https://artifacts.elastic.co/downloads/beats/heartbeat/heartbeat-7.14.1-darwin-x86_64.tar.gz
tar xzvf heartbeat-7.14.1-darwin-x86_64.tar.gz
cd heartbeat-7.14.1-darwin-x86_64/
步驟二:修改配置檔案heartbeat.yml。
output.elasticsearch:
  hosts: ["localhost:9200"]
  username: "elastic"
  password: "<password>"
setup.kibana:
  host: "localhost:5601"
步驟三:启动heartbeat
./heartbeat setup #建立索引
./heartbeat -e # 啟動
為了使 heartbeat 知道要檢查那些服務,所以可以在heartbeat.yml設定要檢查的服務項目,如下面範例:
# 監控服務設定
heartbeat.monitors:
- type: http # 使用http方式監控,可以为TCP、icmp等
  enabled: true
  id: my-monitor
  name: My Monitor
  urls: ["http://localhost:9200"]
  schedule: '@every 10s'  # 每 10s 抓取一次
在heartbeat安裝目錄下monitors.d的目錄,底下的每個yml檔都可以是一個可監控的服務文件。
# 副檔名為 .disabled 是還沒啟用的設定
$ ls monitors.d/
sample.http.yml.disabled sample.icmp.yml.disabled sample.tcp.yml.disabled
我們來看一下monitors.d的目錄下 .yml的內容。
- type: http
  id: it13-http-monitor
  name: it13 HTTP Monitor
  schedule: '@every 5s' # every 5 seconds from start of beat
  hosts: ["http://localhost:8080/api/ping"]
  check.request:
    method: GET
  check.response:
    body: "PING PONG" # 檢查回應的字串要等於"PING PONG"
  ipv4: true
  ipv6: true
  mode: any
接下來我們只要重新啟重heartbeat後,就可以到kibana中查看服務運行的狀態。